package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.typehandler.EntityCcbPosInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.payconfig.CcbPosDeviceRequest;
import com.ocom.common.request.payconfig.GetCcbPosListRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.payconfig.GetCcbNoPosDeviceListResponse;
import com.ocom.common.response.payconfig.GetCcbPosListResponse;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoCcbPosInfo;
import com.ocom.payconfig.service.CcbPosInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 智慧生活柜台信息 服务实现类
 * </p>
 *
 * @author js
 * @since 2023-10-04
 */
@Service
public class CcbPosInfoServiceImpl extends ServiceImpl<IDaoCcbPosInfo, EntityCcbPosInfo> implements CcbPosInfoService {

    @Resource
    private IDaoCcbPosInfo iDaoCcbPosInfo;

        @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
        public Result bindCcbPosDevice(Long comId,CcbPosDeviceRequest request){
            QueryWrapper<EntityCcbPosInfo> query = Wrappers.query();
            query.eq("com_id",request.getComId());
            query.eq("device_no",request.getDeviceNo());
            query.select("id","vpos_id");
            query.last("limit 1");
            EntityCcbPosInfo posInfo = this.getOne(query);
            if(posInfo != null){
                return new Result(501,"设备已绑定到虚拟柜台号："+ posInfo.getVposId(), null);
            }

            query.clear();
            query.eq("com_id",request.getComId());
            query.eq("vpos_id",request.getVposId());
            query.select("id","vpos_id");
            query.last("limit 1");
            EntityCcbPosInfo vPosInfo = this.getOne(query);
            if(vPosInfo == null){
                return new Result(501,"虚拟柜台号不存在", null);
            }
            UpdateWrapper<EntityCcbPosInfo> update = Wrappers.update();
            update.set("device_no",request.getDeviceNo());
            update.eq("com_id",request.getComId());
            update.eq("vpos_id",request.getVposId());

            //test_js 修改分库2.3.9设备信息表xf_device_info中comId、deviceNo匹配记录的字段？？？
            return ResultUtil.success(this.update(update));
        }

        @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
        public Result unbindCcbPosDevice(Long comId,CcbPosDeviceRequest request){
            QueryWrapper<EntityCcbPosInfo> query = Wrappers.query();
            query.eq("com_id",request.getComId());
            query.eq("device_no",request.getDeviceNo());
            query.select("id","device_no");
            query.last("limit 1");
            EntityCcbPosInfo posInfo = this.getOne(query);
            if(posInfo == null){
                return new Result(501,"设备未找到。", null);
            }
            UpdateWrapper<EntityCcbPosInfo> update = Wrappers.update();
            update.set("device_no","");
            update.eq("com_id",request.getComId());
            update.eq("device_no",request.getDeviceNo());

            //test_js 2、修改分库2.3.9设备信息表xf_device_info中comId、deviceNo匹配记录的字段？？？
            return ResultUtil.success(this.update(update));
        }

        @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
        public Result getCcbPosList(Long comId,GetCcbPosListRequest request){
            List<GetCcbPosListResponse> list = iDaoCcbPosInfo.getCcbPosList(request.getComId(), request.getBusinessId());
            HashMap<String,List<GetCcbPosListResponse>> map = new HashMap<>();
            map.put("posList", list);
            return ResultUtil.success(map);
        }

        @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
        public Result getCcbNoPosDeviceList(Long comId,GetCcbPosListRequest request){
            if(request.getMerchId()==null){
                return null;
            }
            List<GetCcbNoPosDeviceListResponse> list = null;
            if(request.getMerchId()>0){
                list = iDaoCcbPosInfo.getCcbNoPosDeviceListByThan(request.getComId(), request.getMerchId());
            }else if(request.getMerchId()==0){
                list = iDaoCcbPosInfo.getCcbNoPosDeviceList(request.getComId());
            }
            HashMap<String,List<GetCcbNoPosDeviceListResponse>> map = new HashMap<>();
            map.put("deviceList", list);
            return ResultUtil.success(map);
        }

        public Boolean updateByDeviceNo(Long comId, String deviceNo){
            UpdateWrapper<EntityCcbPosInfo> updatePos = Wrappers.update();
            updatePos.eq("com_id", comId);
            updatePos.eq("device_no", deviceNo);
            updatePos.set("device_no","");
            return this.update(updatePos);
        }

}
